//
//  RegisterViewController.m
//  随拍
//
//  Created by lanou3g on 16/1/22.
//  Copyright © 2016年 Ashoka. All rights reserved.
//

#import "RegisterViewController.h"
#import "RootBarViewController.h"
#import <AVOSCloud/AVOSCloud.h>

@interface RegisterViewController () <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *backImage;
@property (weak, nonatomic) IBOutlet UITextField *userName;
@property (weak, nonatomic) IBOutlet UITextField *passWord;
@property (weak, nonatomic) IBOutlet UITextField *passWord2;
@property (weak, nonatomic) IBOutlet UITextField *emelName;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self addTextField:self.userName ImageView:@"1.jpg" placeholder:@"输入用户名" clearButtonMode:UITextFieldViewModeWhileEditing];
    [self addTextField:self.passWord ImageView:@"1.jpg" placeholder:@"输入密码" clearButtonMode:UITextFieldViewModeWhileEditing];
    [self addTextField:self.passWord2 ImageView:@"1.jpg" placeholder:@"确认密码" clearButtonMode:UITextFieldViewModeWhileEditing];
    [self addTextField:self.emelName ImageView:@"1.jpg" placeholder:@"输入邮箱" clearButtonMode:UITextFieldViewModeWhileEditing];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)registerAction:(UIButton *)sender {
    
    BOOL emilName = [self validateEmail:self.emelName.text];
    NSLog(@"%d",emilName);
    if (emilName) {
        AVUser *user = [AVUser user];
        user.username = self.userName.text;
        user.password = self.passWord.text;
        user.email = self.emelName.text;
        [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                NSLog(@"成功");
                [self presentViewController:[RootBarViewController new] animated:YES completion:nil];
            }else{
                UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"输入错误" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                    NSLog(@"取消");
                }];
                UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                    
                    NSLog(@"点击了确定按钮");
                }];
                
                [alertVc addAction:cancle];
                
                [alertVc addAction:confirm];
                [self presentViewController:alertVc animated:YES completion:^{
                    
                    nil;
                    
                }];
            }
        }];
        
    }else{
        NSLog(@"粗无");
    }
    
    
}

- (void)addTextField:(UITextField *)textField ImageView:(NSString *)imageView placeholder:(NSString *)placehoder clearButtonMode:(UITextFieldViewMode)clearButton{
    
    UIImageView *leftImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageView]];
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.leftView = leftImageView;
    textField.placeholder = placehoder;
    textField.delegate = self;
    textField.clearButtonMode = clearButton;
}

//邮箱
- (BOOL) validateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}


//手机号码验证
- (BOOL) validateMobile:(NSString *)mobile
{
    //手机号以13， 15，18开头，八个 \d 数字字符
    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:mobile];
}


//车牌号验证
- (BOOL) validateCarNo:(NSString *)carNo
{
    NSString *carRegex = @"^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$";
    NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];
    NSLog(@"carTest is %@",carTest);
    return [carTest evaluateWithObject:carNo];
}


//车型
+ (BOOL) validateCarType:(NSString *)CarType
{
    NSString *CarTypeRegex = @"^[\u4E00-\u9FFF]+$";
    NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CarTypeRegex];
    return [carTest evaluateWithObject:CarType];
}


//用户名
- (BOOL) validateUserName:(NSString *)name
{
    NSString *userNameRegex = @"^[A-Za-z0-9]{6,20}+$";
    NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];
    BOOL B = [userNamePredicate evaluateWithObject:name];
    return B;
}


//密码
- (BOOL) validatePassword:(NSString *)passWord
{
    NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
    return [passWordPredicate evaluateWithObject:passWord];
}


//昵称
+ (BOOL) validateNickname:(NSString *)nickname
{
    NSString *nicknameRegex = @"^[\u4e00-\u9fa5]{4,8}$";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
    return [passWordPredicate evaluateWithObject:nickname];
}


//身份证号
- (BOOL) validateIdentityCard: (NSString *)identityCard
{
    BOOL flag;
    if (identityCard.length <= 0) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [identityCardPredicate evaluateWithObject:identityCard];
}

@end
